/**
 * 供派生的Controller基类，以获得公用的方法
 * Market 3.0 support
 * Created by zhanggj
 * At 2013-4-12
 */
package com.chouxiaozi.wechatpublic.controller;

import com.chouxiaozi.wechatpublic.commons.CoreConsts;
import com.chouxiaozi.wechatpublic.commons.JsonResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.URLDecoder;

public class BaseController {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 返回正确的JSON数据的包装方法，数据都有固定的格式 返回格式：{"retcode":0, "retmsg":"", "retdata":
	 * xxx}
	 */
	public JsonResult wrapSuccessResult(Object retdata) {
		return this.wrapResult(CoreConsts.RESULT_CODE_SUCCESS, "", retdata);
	}

	/**
	 * 返回缺省的错误JSON数据的包装方法，数据都有固定的格式 返回格式：{"retcode":-1, "retmsg":"未知错误",
	 * "retdata": null}
	 */
	public JsonResult wrapFailResult() {
		return this.wrapResult(CoreConsts.RESULT_CODE_ERROR, "未知错误", null);
	}

	/**
	 * 返回缺省的错误JSON数据的包装方法，数据都有固定的格式 返回格式：{"retcode":-1, "retmsg":"未知错误",
	 * "retdata": null}
	 */
	public JsonResult wrapFailResult(String retmsg) {
		return this.wrapResult(CoreConsts.RESULT_CODE_ERROR, retmsg, null);
	}
	
	/**
	 * 返回通用的返回JSON数据的包装方法，数据格式都有固定的格式 返回格式：{"retcode":x, "retmsg":xx, "retdata":
	 * xxx}
	 */
	public JsonResult wrapFailResult(int retcode, String retmsg) {
		JsonResult ret = new JsonResult();
		ret.setRetcode(retcode);
		ret.setRetmsg(retmsg);
		ret.setRetdata(null);
		return ret;
	}

	/**
	 * 返回通用的返回JSON数据的包装方法，数据格式都有固定的格式 返回格式：{"retcode":x, "retmsg":xx, "retdata":
	 * xxx}
	 */
	public JsonResult wrapResult(int retcode, String retmsg, Object retdata) {
		JsonResult ret = new JsonResult();
		ret.setRetcode(retcode);
		ret.setRetmsg(retmsg);
		ret.setRetdata(retdata);
		return ret;
	}

	/**
	 * 取request对象
	 */
	public HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	/**
	 * 取session对象
	 */
	public HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 获取请求参数
	 */
	public String getParameter(String name) {
		return this.getRequest().getParameter(name);
	}

	/**
	 * 取页面传递的页数
	 */
	public int getPageNo() {
		String pageNo = this.getRequest().getParameter("pageNo");
		if (StringUtils.isEmpty(pageNo)) {
			return 1;
		}
		int no = Integer.parseInt(pageNo);
		return no > 0 ? no : 1;
	}

	/**
	 * 取页面传递的最大记录数
	 */
	/*public int getPageSize() {
		String pageSize = this.getRequest().getParameter("pageSize");
		if (StringUtils.isEmpty(pageSize)) {
			pageSize = (String) this.getRequest().getSession().getAttribute("pageSize");
			if (StringUtils.isEmpty(pageSize)) {
				return Consts.MAX_PAGE_SIZE;
			}
		}
		return Integer.parseInt(pageSize);
	}*/

	/**
	 * 统一的返回消息页面
	 */
	public String returnMessage(String message) {
		this.getRequest().setAttribute("message", message);
		return "/commons/message";
	}

	/**
	 * 返回重定向路径
	 * 
	 * @param path
	 * @return
	 */
	public String getRedirectPath(String path) {
		return "redirect:/do" + path;
	}

	/**
	 * 获取访问基本路径
	 * 
	 * @return
	 */
	public String getBasePath() {
		String path = getRequest().getContextPath();
		String basePath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":"
				+ getRequest().getServerPort() + path;
		return basePath;
	}

	/**
	 * 根据参数名从HttpRequest中获取String类型的参数值，无值则返回"" .
	 * 
	 * @param key
	 *            .
	 * @return String .
	 */
	public String getString_UrlDecode_UTF8(String key) {
		try {
			return URLDecoder.decode(this.getParameter(key), "utf-8");
		} catch (Exception e) {
			return "";
		}

	}
	
	public final static String ERROR_JSP_PAGE = "/error";
	
	public String return400Page(){
		
		return ERROR_JSP_PAGE+"/400";
	}

	public String return404Page(){
		
		return ERROR_JSP_PAGE+"/404";
	}
	
	public String return500Page(){
		
		return ERROR_JSP_PAGE+"/500";
	}
}
